<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include	file="/WEB-INF/classes/com/witframework/webroot/framework/common/taglibs.jsp"%>
<%@ taglib prefix="tab" uri="http://ditchnet.org/jsp-tabs-taglib" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>entityMeta</title>
		<%@ include	file="/WEB-INF/classes/com/witframework/webroot/framework/common/meta.jsp"%>
		<%@ include	file="/WEB-INF/classes/com/witframework/webroot/framework/common/componentsmeta.jsp"%>
		<%@ include	file="/WEB-INF/classes/com/witframework/webroot/framework/common/validator.jsp"%>
		<tab:tabConfig />
		<script type="text/javascript" src="${contextPath}/dwr/interface/NameChecker.js"></script>
		<script type="text/javascript" src="${contextPath}/dwr/interface/HibernateValidator.js"></script>
		<script type="text/javascript">
			function showInputType(id,show){				
				if(show=="Ref")
					document.getElementById(id).style.display="block";
				else
					document.getElementById(id).style.display="none";       	
			}
		</script>
	</head>
	<body class="content">
		<div class="page-navigate" >
			<img src="${contextPath}/webresource/images/meta/unit.png">
			<span>
				应用开发-<a href="${contextPath}/metaAppController.wit">
				应用</a>-<a href="${contextPath}/metaModuleController.wit?app_id=<wit:param name="app_id"/>">
				模块</a>-<a href="${contextPath}/classpath:/com/witframework/meta/view/Unit/wizard1.jsp.wit?app_id=<wit:param name="app_id"/>&module=<wit:param name="module"/>">
				单元</a>-<a href="${contextPath}/metaEntityController.wit?app_id=<wit:param name="app_id"/>&module=<wit:param name="module"/>">
				创建数据表和实体类</a>-
				新增
			</span>
		</div>		
		<form name="entityMetaForm" action="metaEntityController!store.wit" method="post"
			focus="className" style="margin-bottom:0;" id="metaEntityForm" >
			<div align=right class="fix-operation" >
				<a href="#" onclick="entityMetaForm.submit()" accesskey="a">
				<img src="${contextPath}/webresource/images/save2.png">
				保存(S)</a>
				<a href="javascript:delItem(document.entityMetaForm,'metaEntityController!delete.wit?itemlist=${entityMeta.id}&module=<c:choose><c:when test="${entityMeta.module==null}"><wit:param name="module"/></c:when><c:otherwise>${entityMeta.module}</c:otherwise></c:choose>')" accesskey="a">
				<img src="${contextPath}/webresource/images/ico_btn_del.gif">
				删除(S)</a>
			</div>				
			<tab:tabContainer id="foo-bar-container" selectedTabPaneId="base">
				<tab:tabPane id="base" tabTitle="实体">
						<input name="entityMeta_id" value="${entityMeta.id}" type="hidden">
						<input name="entityMeta_versionNum" value="${entityMeta.versionNum}" type="hidden">
						
						<input name="entityMeta_module" value="<wit:chooseNotEmpty parameter="entityMeta.module;module"/>" type="hidden">
						<input name="module" value="<wit:chooseNotEmpty parameter="entityMeta.module;module"/>" type="hidden">
						<table class="formtable">
							<tr>
								<td>
									<label for="entityMeta_displayName">名称</label>
								</td>
								<td>
									<input type="text" name="entityMeta_displayName" value="${entityMeta.displayName}" >
								</td>
							</tr>
							<tr>
								<td>
									<label for="entityMeta_className">类名称</label>
								</td>
								<td>
									<input type="text" size="50" name="entityMeta_className" value="${entityMeta.className}" require="true" datatype="require" msg="此项不能为空">
								</td>
							</tr>
							
							<tr>
								<td>
									<label for="entityMeta_tableName">表名</label>
								</td>
								<td>
									<input type="text" name="entityMeta_tableName" value="${entityMeta.tableName}" >
								</td>
							</tr>
							<tr>
								<td>
									<label for="entityMeta_hasVersion">数据一致性</label>
								</td>
								<td>
									<input type="checkbox" name="entityMeta_hasVersion" value="versionNum" ${fn:contains(entityMeta.hasVersion,"versionNum")?"checked":""} }>版本
								</td>
							</tr>
							<tr>
								<td >
									<label for="entityMeta_description">说明</label>
								</td>
								<td>
									<textarea name="entityMeta_description" >${entityMeta.description}</textarea>
								</td>
							</tr>
						</table>
				</tab:tabPane>
				<tab:tabPane id="adv" tabTitle="属性">						
						<table class="formtable" id="colsTable">
						<tbody>
							<thead>
								<tr>
									<td><br /></td>
									<td class="operation" colspan="11" align="right">
									<a href="#" onclick="addRow()" accesskey=a>
									<img src="${contextPath}/webresource/images/tool/add.png" >
									增加(A)</a>
									<a href="#" onclick="delRow()" accesskey=r>
									<img src="${contextPath}/webresource/images/tool/remove.png">
									移除(R)</a></td>
								</tr>
							</thead>
							<tr class="H">
								<td><br /></td>
								<td><input type="checkbox" onclick="slectAll('C',this.checked);"></td>								
								<td>名称</td>
								<td>属性名(代码)</td>
								<td>数据类型</td>
								<!-- 
								<td><img src="${contextPath}/webresource/images/pk.png"></td>
								 -->
								<td><img src="${contextPath}/webresource/images/notnull.png"></td>
								<td>长度</td>
								<td>精度</td>
								<td>数值范围</td>
								<td>有效</td>
								<td>顺序</td>
							</tr>

							<c:forEach var="entityFieldMeta" items="${entityMeta.fields}" varStatus="status">
							<tr class="T" id="r${status.index+1}">
								<td class="XH">${status.index+1}</td>
								<td>
									<input type="checkbox" class="C" name="r${status.index+1}_rowindex" value="${status.index+1}" id="r${status.index+1}_rowindex"
										onchange="setCheckedRow(this.value,this.checked)">
									<input type="hidden" name="r${status.index+1}_entityFieldMeta_id" value="${entityFieldMeta.id}">
								</td>
								<td><input type="text" maxlength="30"  name="r${status.index+1}_entityFieldMeta_displayName" value="${entityFieldMeta.displayName}"></td>
								<td><input type="text" maxlength="30"  name="r${status.index+1}_entityFieldMeta_name" value="${entityFieldMeta.name}"><span class="ER">*</span></td>
								
								<td>
									<wit:select name="r${status.index+1}_entityFieldMeta_typeName"
										 data="com.witframework.meta.EntityFieldType" datatype="class"										
										 defaultValue="${entityFieldMeta.typeName}" 
										 onchange="showInputType('div${status.index+1}',this.value);"/>
									<div id="div${status.index+1}" style="display:none;">
									<input  name="r${status.index+1}_entityFieldMeta_refEntityId" id="r${status.index+1}_entityFieldMeta_refEntityId" type="hidden">
									<input  name="r${status.index+1}_entityFieldMeta_refEntityName" id="r${status.index+1}_entityFieldMeta_refEntityName">
									<input  name="r${status.index+1}_entityFieldMeta_refEntityClass" id="r${status.index+1}_entityFieldMeta_refEntityClass" type="hidden">
									<wit:selectTreeDialog treeid="refTree${status.index+1}" data="select id,classname,displayname from Meta_Entity where id !='${entityMeta.id}'"
									data2="select id,name from Meta_Module" datatype="sql" 
									root="0" parentid="module" others="classname"
									field1="r${status.index+1}_entityFieldMeta_refEntityId"
									field2="r${status.index+1}_entityFieldMeta_refEntityName" field3="r${status.index+1}_entityFieldMeta_refEntityClass"
									nodeid="id" nodelabel="displayname" nodeid2="id" nodelabel2="name" 
									/></div>						
								</td>
								<!-- 
								<td><input type="checkbox" value="Y" name="r${status.index+1}_entityFieldMeta_isIdentity" <c:if test="${fn:contains(entityFieldMeta.isIdentity,\"Y\")}">checked</c:if> ></td>
								 -->
								<td><input type="checkbox" value="Y" name="r${status.index+1}_entityFieldMeta_isNullable" <c:if test="${fn:contains(entityFieldMeta.isNullable,\"Y\")}">checked</c:if> ></td>
								<td><input type="text" maxlength="8" size="3" name="r${status.index+1}_entityFieldMeta_length" value="${entityFieldMeta.length}"></td>
								<td><input type="text" maxlength="8" size="3" name="r${status.index+1}_entityFieldMeta_precision" value="${entityFieldMeta.precision}"></td>
								<td><input type="text" maxlength="20" size="3" name="r${status.index+1}_entityFieldMeta_scale" value="${entityFieldMeta.scale}"></td>
								<td><input type="checkbox" value="Y" name="r${status.index+1}_entityFieldMeta_isactive" <c:if test="${fn:contains(entityFieldMeta.isactive,\"Y\")}">checked</c:if>></td>
								<td><input type="text" maxlength="8" size="3" name="r${status.index+1}_entityFieldMeta_sortnum" value="${entityFieldMeta.sortnum}"></td>
							</tr>
							</c:forEach>
							<c:if test="${fn:length(entityMeta.fields)==0}">
							<tr class="T" id="r${fn:length(entityMeta.fields)+1}">
								<td class="XH">${fn:length(entityMeta.fields)+1}</td>
								<td>
									<input type="checkbox" class="C" name="r${fn:length(entityMeta.fields)+1}_rowindex" value="${fn:length(entityMeta.fields)+1}" id="r${fn:length(entityMeta.fields)+1}_rowindex"
										onchange="setCheckedRow(this.value,this.checked)">
									<input type="hidden" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_id" >
								</td>
								<td><input type="text" maxlength="30"  name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_display" ></td>
								<td><input type="text" maxlength="30"  name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_name" ><span class="ER">*</span></td>
								<td>
									<wit:select name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_typeName"
										 data="com.witframework.meta.EntityFieldType" datatype="class" defaultValue="String"
										 onchange="showInputType('div${status.index+1}',this.value);"/>
									
									<div id="div${status.index+1}" style="display:none;">
									<input  name="RefId${status.index+1}" id="RefId${status.index+1}" type="hidden">
									<input  name="r${status.index+1}_entityFieldMeta_refEntityName" id="r${status.index+1}_entityFieldMeta_refEntityName">
									<input  name="Ref${status.index+1}" id="Ref${status.index+1}" type="hidden">
									<wit:selectTreeDialog treeid="refTree${status.index+1}" data="select id,classname,displayname from Meta_Entity where id !='${entityMeta.id}'"
									data2="select id,name from Meta_Module" datatype="sql" 
									root="0" parentid="module" others="classname"
									field1="RefId${status.index+1}"
									field2="r${status.index+1}_entityFieldMeta_refEntityName" field3="Ref${status.index+1}"
									nodeid="id" nodelabel="displayname" nodeid2="id" nodelabel2="name" 
									/></div>
																								
								</td>
								<!-- 
								<td><input type="checkbox" value="true" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_isIdentity"></td>
								 -->
								<td><input type="checkbox" value="true" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_isNullable"></td>
								<td><input type="text" maxlength="8" size="3" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_length"></td>
								<td><input type="text" maxlength="8" size="3" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_precision"></td>
								<td><input type="text" maxlength="20" size="3" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_scale"></td>
			<td><input type="checkbox" checked value="true" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_isValid"></td>
			<td><input type="text" maxlength="8" size="3" name="r${fn:length(entityMeta.fields)+1}_entityFieldMeta_sortnum" value="10"></td>
							</tr>
							</c:if>							
						</tbody>
					</table>					
					<c:choose>
						<c:when test="${fn:length(entityMeta.fields)==0}">
							<input name="rowcount" id="rowcount" value="1" type="hidden"/>
						</c:when>
						<c:otherwise>
							<input name="rowcount" id="rowcount" value="${fn:length(entityMeta.fields)}" type="hidden"/>
						</c:otherwise>
					</c:choose>					
				</tab:tabPane>				
			</tab:tabContainer>
		</form>		
		<script>
		$(document).ready(function(){
  			$("entityMeta_className").focus();
  			checkClassName("entityMeta_className");
			//validateField("com.witbyte.meta.entity.EntityMeta");

		});
		//$('#entityMetaForm').checkForm();

		var checkedrow="";
		//选中或取消选中所有的行
		function slectAll(classname,checked){
			rowIndex=1;
			$("."+classname).each(function(){
				this.checked=checked;
				if(checked){
					checkedrow=checkedrow+","+rowIndex;
				}else{
					checkedrow=checkedrow.replace(","+rowIndex,"");
				}
				rowIndex++;
			});
		}
		//新增一行
		function addRow(){
			newRow=$(".T:last").clone();
			var rowIndex=0;
			$(".T:last").find("INPUT:first").each(function(){
       			rowIndex=this.value*1+1;
    		});

			$(".T:last").after(newRow);//通过增加最后一行的Clone完成新增
			$(".XH:last").html(rowIndex);//修改总行数
			//设置新增行的标识值
			$(".T:last").find("INPUT:first").each(function(){
       			this.value=rowIndex
       			this.checked=false;//默认不选中
    		});
    		$(".T:last").find("INPUT:eq(1)").removeAttr("value");
    		//$(".T:last").find("INPUT:eq(1)").each(function(){
       		//	this.value="";
       		//	this.name="r"+rowIndex+"_entityFieldMeta_id";
    		//});
    		//设置新增行中的字段排序值
    		$(".T:last").find("INPUT:last").each(function(){
       			this.value=(rowIndex)+"0"
    		});
    		$(".T:last").attr("id","r"+rowIndex);

    		$(".T:last").find("INPUT").each(function(){
    			name=this.name.substring(this.name.indexOf("_")>-1?this.name.indexOf("_"):0);
       			this.name="r"+rowIndex+name;
       			this.id="r"+rowIndex+name;
    		});
    		$(".T:last").find(".div").each(function(){    			
       			alert(roeIndex);
       			this.id="div"+rowIndex;
    		});
    		$("#rowcount").attr("value",rowIndex);
		}

		function setCheckedRow(rowid,checked){
			if(checked){
				checkedrow=checkedrow+","+rowid;
			}else{
				checkedrow=checkedrow.replace(","+rowid,"");
			}
		}
		function delRow(){
			delrows=checkedrow.split(",");
			for(i=1;i<delrows.length;i++){
				if(delrows[i]*1>1)
					$("#r"+delrows[i]).each(function(){$(this).remove();});
			}
			checkedrow="";
			rowIndex=1;
			$(".T").find("INPUT").each(function(){
    			name=this.name.substring(this.name.indexOf("_")>-1?this.name.indexOf("_"):0);
       			this.name="r"+rowIndex+name;
       			this.id="r"+rowIndex+name;       			
    		});
    		rowIndex=1;
			$(".T").each(function(){
				$(this).find("INPUT:first").each(function(){
    				this.value=rowIndex;
       				rowIndex++;
       			});
    		});
    		rowIndex=1;
    		$(".T").each(function(){
    			$(this).attr("id","r"+rowIndex);
    			rowIndex++;
    		});
    		rowIndex=1;
    		$(".XH").each(function(){
    			$(this).html(rowIndex)
    			rowIndex++;
    		});
    		$("#rowcount").attr("value",rowIndex-1);
		}
		</script>
		<script>window.parent.frames.menu.selectTree("${entityMeta.id}");</script>
	</body>
</html>